package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhouwei
 * @date 2020-01-16 18:50
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //调用分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page= permissionDao.findPage(queryPageBean);
        long total = page.getTotal();
        List<Map> list =new ArrayList<>();
        List<Permission> result = page.getResult();
        for(Permission permission:result){
            Set<Role> roles = permission.getRoles();
            for(Role role:roles){
                role.getName();
                Map map=new HashMap<>();
                map.put("username",role.getName());
                map.put("name",permission.getName());
                map.put("keyword",permission.getKeyword());
                map.put("description",permission.getDescription());
                list.add(map);
            }
        }
        PageResult pageResult = new PageResult(total, list);
        return pageResult;
    }
}
